package gov.pbc.nn.kjgl.maintenance.view;

import gov.pbc.nn.core.dao.CommonHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.core.utils.GetTableUtils;
import gov.pbc.nn.kjgl.maintenance.entity.SysChange;
import gov.pbc.nn.kjgl.maintenance.handler.SysBusinessPeopleAssignmentHandler;
import gov.pbc.nn.kjgl.maintenance.handler.SysOperatorAssignmentHandler;
import gov.pbc.nn.kjgl.maintenance.view.SysChangePR.SysChangeNode;
import gov.pbc.nn.uflo.UfloUtils;
import gov.pbc.nn.uflo.entity.Opinion;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.uflo.client.service.ProcessClient;
import com.bstek.uflo.client.service.TaskClient;
import com.bstek.uflo.model.task.Task;

/**
 * 
 * @author Administrator
 * 
 */
@Component
public class SysChangeAuditPR {

	@Resource(name = CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao hibernateDao;
	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;
	@Resource(name = ProcessClient.BEAN_ID)
	private ProcessClient pc;
	@Resource(name = TaskClient.BEAN_ID)
	private TaskClient tc;

	/**
	 * 获取意见
	 * 
	 * @param params
	 * @return
	 */
	@DataProvider
	public List<Opinion> loadSysChangeYwyOpinion(Map<String, Object> params) {
		long taskId = Long.parseLong(params.get("taskId").toString());
		String businessId = (String) params.get("businessId");
		Task task = this.tc.getTask(taskId);
		String sysId = (String) this.pc.getProcessVariable("sysId", task.getProcessInstanceId());
		String nodeName = task.getNodeName();
		String processName = this.pc.getProcessById(task.getProcessId()).getName();
		if (SysChangeNode.OperationStartProcess.PROCESS_NAME.equals(processName)) {
			SysBusinessPeopleAssignmentHandler handler = ContextHolder
					.getBean(SysBusinessPeopleAssignmentHandler.BEAN_ID);
			List<String> users = handler.getBusinessPeople(sysId);
			if (SysChangeNode.OperationStartProcess.NODE_5.equals(nodeName)) {
				List<Opinion> list = UfloUtils.getLatestOpinions(task.getProcessInstanceId(), businessId,
						SysChangeNode.OperationStartProcess.NODE_4, null, users);
				return list;
			}
		} else {
			if (SysChangeNode.BusinessStartProcess.NODE_5.equals(nodeName)
					|| SysChangeNode.BusinessStartProcess.NODE_6.equals(nodeName)) {
				SysOperatorAssignmentHandler handler = ContextHolder.getBean(SysOperatorAssignmentHandler.BEAN_ID);
				List<Opinion> list = UfloUtils.getLatestOpinions(task.getProcessInstanceId(), businessId,
						SysChangeNode.BusinessStartProcess.NODE_4, null, handler.getOperators(sysId));
				return list;
			} else if (SysChangeNode.BusinessStartProcess.NODE_11.equals(nodeName)) {
				SysBusinessPeopleAssignmentHandler handler = ContextHolder
						.getBean(SysBusinessPeopleAssignmentHandler.BEAN_ID);
				List<String> users = handler.getBusinessPeople(sysId);
				List<Opinion> list = UfloUtils.getLatestOpinions(task.getProcessInstanceId(), businessId,
						SysChangeNode.BusinessStartProcess.NODE_10, null, users);
				return list;
			}
		}
		return null;
	}

	@Expose
	@Transactional
	public void completeTaskByAuditOpinion(Map<String, Object> params) {
		long taskId = Long.parseLong(params.get("taskId").toString());
		String opinion = (String) params.get("opinion");
		String businessId = (String) params.get("businessId");
		int agree = Integer.parseInt(params.get("agree").toString());
		int backFlow = Integer.parseInt(params.get("backFlow").toString());
		List<String> backUsers = (List<String>) params.get("backUsers");
		Map<String, Object> variables = new HashMap<String, Object>();

		Task task = this.tc.getTask(taskId);
		String processName = this.pc.getProcessById(task.getProcessId()).getName();
		String nodeName = task.getNodeName();
		if (processName.equals(SysChangeNode.OperationStartProcess.PROCESS_NAME)) {
			// 运维部门提出系统变更
			if (SysChangeNode.OperationStartProcess.NODE_5.equals(nodeName)) {
				SysChange sysChange = this.hibernateDao.get(businessId, SysChange.class);
				sysChange.setYwbmfzrDate(new Date());
				sysChange.setYwbmfzrOpinion(opinion);
				this.hibernateDao.update(sysChange);
				if (agree == 1) {
					variables.put("taskname", "运维部门申请【" + sysChange.getSysName() + "】变更，业务主管部门同意变更");
				} else if (agree == 0) {
					if(backFlow==1){
						variables.put("taskname", "运维部门申请【" + sysChange.getSysName() + "】变更，请业务员重新填写意见");
					}else{
						variables.put("taskname", "运维部门申请【" + sysChange.getSysName() + "】变更业务部门退回，需要根据实际情况调整");
					}
				}
			}

		} else {
			// 运维部门提出系统变更
			if (SysChangeNode.BusinessStartProcess.NODE_5.equals(nodeName)) {
				SysChange sysChange = this.hibernateDao.get(businessId, SysChange.class);
				sysChange.setJsOpinion(opinion);
				sysChange.setJsDate(new Date());
				this.hibernateDao.update(sysChange);
			} else if (SysChangeNode.BusinessStartProcess.NODE_6.equals(nodeName)) {
				SysChange sysChange = this.hibernateDao.get(businessId, SysChange.class);
				sysChange.setYunweibmfzrDate(new Date());
				sysChange.setYunweibmfzrOpinion(opinion);
				this.hibernateDao.update(sysChange);
			}
		}
		Opinion o = new Opinion();
		o.setBusKey(businessId);
		o.setBusTable(GetTableUtils.getTablename(SysChange.class));
		o.setOpinion(opinion);
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setProcessName(processName);
		o.setTaskId(taskId);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		this.coreHibernateDao.save(o);

		variables.put("agree", agree);
		variables.put("backFlow", backFlow);
		variables.put("backUsers", backUsers);
		this.tc.start(taskId);
		this.tc.complete(taskId, variables);
	}

	@Expose
	@Transactional
	public void completeTask(Map<String, Object> params) {
		long taskId = Long.parseLong(params.get("taskId").toString());
		String opinion = (String) params.get("opinion");
		String businessId = (String) params.get("businessId");
		int agree = Integer.parseInt(params.get("agree").toString());
		Task task = this.tc.getTask(taskId);
		SysChange sysChange = this.hibernateDao.get(businessId, SysChange.class);
		Map<String, Object> variables = new HashMap<String, Object>();
		String processName = this.pc.getProcessById(task.getProcessId()).getName();
		String nodeName = task.getNodeName();
		if (processName.equals(SysChangeNode.OperationStartProcess.PROCESS_NAME)) {
			// 运维部门提出系统变更
			if (SysChangeNode.OperationStartProcess.NODE_2.equals(nodeName)) {
				sysChange.setJsOpinion(opinion);
				sysChange.setJsDate(new Date());
				this.hibernateDao.update(sysChange);

				String operationDeptLeader = (String) params.get("operationDeptLeader");// 运维部门主管
				if (StringUtils.isNotBlank(operationDeptLeader)) {
					variables.put("operationDeptLeader", operationDeptLeader);
					variables.put("taskname", "运维部门申请【" + sysChange.getSysName() + "】变更，请部门主管审核");
				} else {
					if (agree == 0) {
						variables.put("taskname", "运维部门申请【" + sysChange.getSysName() + "】变更被退回，需要进行调整");
					}

				}
			} else if (SysChangeNode.OperationStartProcess.NODE_3.equals(nodeName)) {
				sysChange.setYunweibmfzrDate(new Date());
				sysChange.setYunweibmfzrOpinion(opinion);
				this.hibernateDao.update(sysChange);
				if (agree == 0) {
					variables.put("taskname", "运维部门申请【" + sysChange.getSysName() + "】变更被退回，需要进行调整");
				} else if (agree == 1) {
					variables.put("taskname", "运维部门申请【" + sysChange.getSysName() + "】变更，部门主管同意变更");
				}
			}

		} else {
			// 业务部门提出系统变更
			if (SysChangeNode.BusinessStartProcess.NODE_4.equals(nodeName)) {
				String cto = (String) params.get("cto");
				if (StringUtils.isNotBlank(cto)) {
					variables.put("cto", cto);// 技术主管
				}
			} else if (SysChangeNode.BusinessStartProcess.NODE_3.equals(nodeName)) {
				sysChange.setYwbmfzrDate(new Date());
				sysChange.setYwbmfzrOpinion(opinion);
				this.hibernateDao.update(sysChange);
			}
		}

		Opinion o = new Opinion();
		o.setBusKey(businessId);
		o.setBusTable(GetTableUtils.getTablename(SysChange.class));
		o.setOpinion(opinion);
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setProcessName(processName);
		o.setTaskId(taskId);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		this.coreHibernateDao.save(o);

		variables.put("agree", agree);
		this.tc.start(taskId);
		this.tc.complete(taskId, variables);
	}
}
